package com.example.demo.controller;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.citypojo;
import com.example.demo.datasource.multi.dao.wuliuDao;
import com.example.demo.datasource.multi.entity.test2quary;
import com.example.demo.datasource.multi.entity.testquary;
import com.example.demo.datasource.multi.service.test2service;
import com.example.demo.datasource.multi.service.testservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by w on 2018/8/30.
 */
@Controller
public class demo {

    @Autowired
    testservice testservice;

    @Autowired
    com.example.demo.datasource.multi.service.test2service test2service;

    @Autowired
    com.example.demo.datasource.multi.dao.wuliuDao wuliuDao;

    @RequestMapping(value= "hello")
    @ResponseBody
    public String test(){
        return "hello ,spring boot!!";
    }

    @RequestMapping("/test")
    @ResponseBody
    public List<testquary> hello(){
        List<testquary> result;
        result= testservice.quarytest();
        return result;

    }

    @RequestMapping("/test2")
    @ResponseBody
    public List<test2quary> hello2(){
        List<test2quary> result;
        result= test2service.quarytest();
        return result;

    }

    @Value("${server.http.port}")
    private String port;

    @RequestMapping("/")
    public String index(Model model, HttpSession session){

        return "wuliu/wuliu";
    }

    @ResponseBody
    @RequestMapping("/getenv")
    public JSONObject getenv(){
        String env;
        if (port.equals("8004")){
            env = "正式环境";
        }else {
            env = "测试环境";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("mag",1);
        map.put("result",env);
        JSONObject result = JSONObject.parseObject(JSON.toJSONString(map));

        return result;
    }

    @ResponseBody
    @RequestMapping("/getcity")
    public JSONObject getcity(){

        String sql = "SELECT * FROM citydata_3level";

        List<citypojo> citylist = wuliuDao.getcity(sql);

        Map<String,Object> map = new HashMap<>();
        map.put("mag",1);
        map.put("result",citylist);
        JSONObject result = JSONObject.parseObject(JSON.toJSONString(map));

        return result;
    }


}
